﻿namespace PoemGame.Domain.Seedwork
{
    public class EntityBase
    {
        public Guid Id { get; protected set; }

        public override bool Equals(object? obj)
        {
            if (obj == null)
            {
                return false;
            }

            var entity= obj as EntityBase;

            if (entity == null)
            {
                return false;
            }

            if (ReferenceEquals(this, obj))
            {
                return true;
            }

            var typeOfEntity1 = GetType();
            var typeOfEntity2 = obj.GetType();
            if (!typeOfEntity1.IsAssignableFrom(typeOfEntity2) && !typeOfEntity2.IsAssignableFrom(typeOfEntity1))
            {
                return false;
            }

            if (!Id.Equals(entity.Id)) return false;

            if(Id.Equals(Guid.Empty)|| entity.Id.Equals(Guid.Empty)) return false;

            return true;
        }

        public override int GetHashCode()
        {
            if(Id.Equals(Guid.Empty)) return new Guid().GetHashCode();
            return Id.GetHashCode();
            
        }
    }

}
